package com.ruoyi.blog.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.blog.dto.config.ConfigForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.blog.domain.TBlogConfig;
import com.ruoyi.blog.service.ITBlogConfigService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 博客基本配置Controller
 * 
 * @author yang
 * @date 2021-11-28
 */
@RestController
@RequestMapping("/config")
public class TBlogConfigController extends BaseController
{
    @Autowired
    private ITBlogConfigService tBlogConfigService;

    /**
     * 查询博客基本配置列表
     */
    @RequiresPermissions("blog:config:list")
    @GetMapping("/list")
    public TableDataInfo list(TBlogConfig tBlogConfig)
    {
        startPage();
        List<TBlogConfig> list = tBlogConfigService.selectTBlogConfigList(tBlogConfig);
        return getDataTable(list);
    }

    /**
     * 导出博客基本配置列表
     */
    @RequiresPermissions("blog:config:export")
    @Log(title = "博客基本配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBlogConfig tBlogConfig) throws IOException
    {
        List<TBlogConfig> list = tBlogConfigService.selectTBlogConfigList(tBlogConfig);
        ExcelUtil<TBlogConfig> util = new ExcelUtil<TBlogConfig>(TBlogConfig.class);
        util.exportExcel(response, list, "博客基本配置数据");
    }

    /**
     * 获取博客基本配置详细信息
     */
    @RequiresPermissions("blog:config:query")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable("configId") Long configId)
    {
        return AjaxResult.success(tBlogConfigService.selectTBlogConfigByConfigId(configId));
    }

    /**
     * 新增博客基本配置
     */
    @RequiresPermissions("blog:config:add")
    @Log(title = "博客基本配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBlogConfig tBlogConfig)
    {
        return toAjax(tBlogConfigService.insertTBlogConfig(tBlogConfig));
    }

    /**
     * 修改博客基本配置
     */
    @RequiresPermissions("blog:config:edit")
    @Log(title = "博客基本配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ConfigForm tBlogConfig)
    {
        if(tBlogConfigService.updateBlogConfig(tBlogConfig)){
            return toAjax(true);
        }else{
            return AjaxResult.error("更新失败，请联系系统管理员！");
        }

    }

    /**
     * 删除博客基本配置
     */
    @RequiresPermissions("blog:config:remove")
    @Log(title = "博客基本配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds)
    {
        return toAjax(tBlogConfigService.deleteTBlogConfigByConfigIds(configIds));
    }

    /**
     * 查询博客基本配置列表
     */
    @GetMapping("/listByType/{configType}")
    public TableDataInfo list(@PathVariable("configType")Integer configType)
    {
        List<TBlogConfig> list = tBlogConfigService.selectTBlogConfigListByType(configType);

        return getDataTable(list);
    }


}
